﻿using M.ShopMachine.Domain;
using M.ShopMachine.Domain.Dto;
using M.ShopMachine.Repository.Interface;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace M.ShopMachine.Repository.Impl
{
    public class BaseWorkshopRepository : BaseRepository<Workshop>, IWorkshopRepository
    {
        public BaseWorkshopRepository(EFDbContext eFDbcontext) : base(eFDbcontext)
        {
        }

        public async Task<int> AddWorkshopAsync(Workshop workshop)
        {
            var id = await base.AddAsync(workshop);
            return id;
        }

        public async Task<Workshop> GetFormWorkshopAsync(int id)
        {
            var allor = await base.GetAsync(id);
            return allor;
        }

        public async Task<List<Workshop>> GetTableWorkShopAsync(string? Number, string? Name, bool? State)
        {
            var gather = await base.GetListAsync(x => x.IsDeleted == false);
            if (Number != null)
            {
                gather = gather.Where(x=>x.WorkshopNumber == Number);
            }
            if (Name != null)
            {
                gather = gather.Where(x => x.WorkshopName.Contains(Name));
            }
            if(State != null)
            {
                gather = gather.Where(x => x.State == State);
            }

            return gather.ToList();
        }

        public async Task<int> UpdWorkShopAsync(Workshop workshop)
        {
            var num = await base.UpdAsync(workshop);
            var id = 0;
            if (num != 0)
            {
                id = workshop.Id;
            }
            return id;
        }

        public async Task<int> UpdStateAsync(int id,bool State)
        {
            var allor = await base.GetAsync(id);
            allor.State = State;
            var num = await base.UpdAsync(allor);
            var idz = 0;
            if (num != 0)
            {
                idz = id;
            }
            return idz;
        }
    }
}
